﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using Autodesk.Revit.DB;
using YJKRevitKernel.RevitDimensionUtility;
using YJKGeometryFuncs.CurveUtility;

namespace YJKPatternSign.StairsRoomPlanSign.ModelLayer
{
	public class OutSideDimensionPacker : IDimensionMake
	{
		public Dimension DetailDimension
		{
			get
			{
				return this.m_DetailDimension;
			}
			private set
			{
				this.m_DetailDimension = value;
			}
		}

		public Dimension GridDimension
		{
			get
			{
				return this.m_GridDimension;
			}
			private set
			{
				this.m_GridDimension = value;
			}
		}

		public OutSideDimensionPacker(List<ReferencePacker> detailDimension, List<ReferencePacker> gridDimension, Line boundLline, DriectionEnum driection)
		{
			this.m_lstDetailDimensionReference = detailDimension;
			this.m_lstGridDimensionReference = gridDimension;
			this.m_boundLline = boundLline;
			this.m_driection = driection;
		}

		public bool TryMakeSign(Autodesk.Revit.DB.Document inputDoc, ViewPlan useView)
		{
			if (this.m_lstDetailDimensionReference != null)
			{
				this.DetailDimension = this.makeDimensionByReference(this.m_lstDetailDimensionReference, CurveUtility.CreateOffsetCurve(this.m_boundLline, -Utility.MMToFeet(StaticResource.DimensionLineOffestValue[0], true)) as Line, useView, inputDoc);
			}
			if (this.m_lstGridDimensionReference != null)
			{
				this.GridDimension = this.makeDimensionByReference(this.m_lstGridDimensionReference, CurveUtility.CreateOffsetCurve(this.m_boundLline, -Utility.MMToFeet(StaticResource.DimensionLineOffestValue[1], true)) as Line, useView, inputDoc);
			}
			return true;
		}

		private Dimension makeDimensionByReference(List<ReferencePacker> inputReference, Line useLine, Autodesk.Revit.DB.View useView, Autodesk.Revit.DB.Document m_doc)
		{
			Dimension result = null;
			if (inputReference.Count < 2)
			{
				return result;
			}
			ReferenceArray referenceArray = new ReferenceArray();
			int num = 0;
			int count = inputReference.Count;
			inputReference = inputReference.Distinct<ReferencePacker>().ToList<ReferencePacker>();
			foreach (ReferencePacker referencePacker in inputReference)
			{
				try
				{
					DetailCurve detailCurve = m_doc.Create.NewDetailCurve(useView, referencePacker.DimLine);
					referenceArray.Append(detailCurve.GeometryCurve.Reference);
				}
				catch (Exception)
				{
					continue;
				}
				num++;
			}
			if (referenceArray.Size >= 2)
			{
				try
				{
					DimensionType dimensionType = RevitDimensionUtiltyMethod.GetDimensionType(m_doc, DimensionTypeEnum.ScaleDimension, null);
					if (dimensionType != null)
					{
						result = m_doc.Create.NewDimension(useView, useLine, referenceArray, dimensionType);
					}
					else
					{
						result = m_doc.Create.NewDimension(useView, useLine, referenceArray);
					}
				}
				catch (Exception)
				{
				}
			}
			return result;
		}

		private DimensionType findWantDimensionType(Autodesk.Revit.DB.Document m_doc)
		{
			DimensionType result = null;
			foreach (Element element in new FilteredElementCollector(m_doc).OfClass(typeof(DimensionType)))
			{
				DimensionType dimensionType = (DimensionType)element;
				if (dimensionType != null && Regex.IsMatch(dimensionType.Name, StaticResource.Str_DimensionTypeName))
				{
					result = dimensionType;
					break;
				}
			}
			return result;
		}

		private double m_ddistanceTolerance = 0.0005;

		private DriectionEnum m_driection;

		private Line m_boundLline;

		private List<ReferencePacker> m_lstDetailDimensionReference;

		private List<ReferencePacker> m_lstGridDimensionReference;

		private Dimension m_DetailDimension;

		private Dimension m_GridDimension;
	}
}
